package com.are;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.PowerManager;
import android.telephony.TelephonyManager;

public class Locator extends Service {
	

	TimerTask mTimerTask;
	private Timer timer = new Timer();
	private static final long UPDATE_INTERVAL = 1000 * 60 * 3;
	private final IBinder mBinder = new LocalBinder();
	final Handler handler = new Handler();
	protected PowerManager.WakeLock wl;
	

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return mBinder;
	}
	
	public class LocalBinder extends Binder {
        Locator getService() {
            // Return this instance of LocalService so clients can call public methods
            return Locator.this;
        }
    }


	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
		wl = pm.newWakeLock( PowerManager.PARTIAL_WAKE_LOCK, "PARTIAL_WAKE_LOCK");
		wl.acquire(); 
		
		mTimerTask = new TimerTask() {
	        public void run() {
	                handler.post(new Runnable() {
						public void run() {
							// TODO Auto-generated method stub
							try {
		    					
		    					System.out.println("Iniciando proceso captura GPS");
		    					
		    					
		    					LocationFinder locationFinder = new LocationFinder();
		    					locationFinder.startFind(Locator.this);
		    					Location location = locationFinder.endFind();


		    					// Using the location find its address
		    					if (location != null) {
		    						EnviarUbicacion(location.getLatitude(),location.getLongitude());

		    					}


		    				} catch (Exception e) {
		    					e.printStackTrace(System.out);
		    				}	
						}
	               });
	        }};
		
		timer.schedule(mTimerTask, 0, UPDATE_INTERVAL);
	
	}
	@Override
	public void onStart(final Intent intent, final int startId) {
		super.onStart(intent, startId);
	}
	private void EnviarUbicacion(final double latitud, final double longitud) {
		new Thread(new Runnable() {
			public void run() {
				TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
				String imei = tm.getDeviceId();
				String url;
				try {
					url = "http://siscos.fcr.com.co:8080/AreApp/SrvGps?imei="
							+ URLEncoder.encode(imei, "UTF-8")
							+ "&latitud="
							+ URLEncoder.encode(
									String.valueOf(latitud),
									"UTF-8")
							+ "&longitud="
							+ URLEncoder.encode(
									String.valueOf(longitud),
									"UTF-8");
					SendLocation conn = new SendLocation(url);
					conn.Send();
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}).start();

	}
	
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (wl != null) {
			wl.release();
		}
		
	}
	
}
